package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CompanyEventsListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CompanyEvents;
import com.isunland.managesystem.entity.CompanyEventsOriginal;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/18 09:51
 */
public class CompanyEventsListFragment extends BaseListFragment {
  protected static final String EXTRA_VALUE =
      CompanyEventsListActivity.class.getSimpleName() + "EXTRA_VALUE";

  private final int REQUST_QUERY = 0;
  private ArrayList<CompanyEvents> mList;
  private String beginoccurrenceTime;
  private String endoccurrenceTime;
  private String chargeCode;
  private String chargeName;
  private String eventCategoryCode;
  private String eventCategoryName;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.companyEvents);
    mList = new ArrayList<>();
    beginoccurrenceTime = MyDateUtil.format(MyDateUtil.getBeforeMonth(3), MyDateUtil.TYPE_DATE);
    endoccurrenceTime = MyDateUtil.format(new Date(), MyDateUtil.TYPE_DATE);
    eventCategoryName = "";
    eventCategoryCode = "";
    chargeCode = "";
    chargeName = "";
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_query:
        //查询
        ExtraValueContent content = new ExtraValueContent();
        content.setExtName1(beginoccurrenceTime);
        content.setExtName2(endoccurrenceTime);
        content.setExtName3(eventCategoryName);
        content.setExtCode3(eventCategoryCode);
        content.setExtName4(chargeName);
        content.setExtCode4(chargeCode);
        Intent query = new Intent(getActivity(), CompanyEventsQueryActivity.class);
        query.putExtra(CompanyEventsQueryFragment.EXTRA_CONTENT, content);
        startActivityForResult(query, REQUST_QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_COMPANY_EVENTS_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("beginoccurrenceTime", beginoccurrenceTime);
    params.put("endoccurrenceTime", endoccurrenceTime);
    params.put("eventCategoryCode", eventCategoryCode);
    params.put("chargeCode", chargeCode);
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    CompanyEventsOriginal original = new Gson().fromJson(json, CompanyEventsOriginal.class);
    ArrayList<CompanyEvents> rows = original.getRows();
    if (Base.SUCCESS == original.getResult() && rows != null) {
      mList.clear();
      mList.addAll(rows);
      CompanyEventsListAdapter mAdapter = new CompanyEventsListAdapter(mActivity, mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    CompanyEvents content = mList.get(position - 1);
    Intent i = new Intent(getActivity(), CompanyEventsPagerActivity.class);
    i.putExtra(EXTRA_VALUE, content);
    startActivity(i);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUST_QUERY) {
      ExtraValueContent mContent =
          (ExtraValueContent) data.getSerializableExtra(CompanyEventsQueryFragment.EXTRA_CONTENT);
      beginoccurrenceTime = mContent.getExtName1();
      endoccurrenceTime = mContent.getExtName2();
      eventCategoryName = mContent.getExtName3();
      eventCategoryCode = mContent.getExtCode3();
      chargeName = mContent.getExtName4();
      chargeCode = mContent.getExtCode4();
      volleyPost();
    }
  }
}
